<template>
	<view class="container">
		<view class="search-bar">
			<uni-search-bar v-model='searchVal' :focus="false" placeholder="查找教练" @confirm="search" />
		</view>

		<swiper class="block" circular indicator-dots autoplay :interval="3000" :duration="1000">
			<swiper-item v-for="item in imgs">
				<image class="img" :src="item"></image>
			</swiper-item>
		</swiper>

		<view class="section-title">热门教练推荐</view>
		<view class="hot">
			<view v-for="(item, index) in trainerList" :key="index" class="card" @click="toDetail(1, item)">
				<image class="jiaolian" :src="addBase(item.avatar)"></image>
				<view class="info">
					<p class="name">姓名：{{ item.username }}</p>
					<p>身高：{{ item.height }}cm</p>
					<p>体重：{{ item.weight }}kg</p>
				</view>
			</view>
		</view>

		<view class="section-title">健身指南</view>
		<view class="guide">
			<view v-for="(item, index) in guideList" :key="index" class="list-item" @click="toDetail(2, item)">
				<image src="../../static/background.png" class="viewImg"></image>
				<view class="line">
					<p class="title">{{ item.title }}</p>
					<p class="three" v-html="item.content"></p>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addBase
	} from '../../utils/myFunction';
	import request from '../../utils/request';
	export default {
		data() {
			return {
				imgs: ['../../static/background.png', '../../static/background.png', '../../static/background.png',
					'../../static/background.png'
				],
				searchVal: null,
				trainerList: [],
				guideList: []
			}
		},
		onLoad() {
			request({
				url: 'system/usr/list',
				method: 'GET',
				data: {
					// 后端支持分页，只要俩个热门
					pageSize: 2,
					role: '教练'
				}
			}).then(res => {
				this.trainerList = res.rows
			})
			request({
				url: 'system/guide/list',
				method: 'GET'
			}).then(res => {
				this.guideList = res.rows
			})
		},
		methods: {
			search() {
				uni.reLaunch({
					url: `/pages/trainer/trainer?searchVal=${this.searchVal}`,
				})
			},
			toDetail(num, item) {
				if (num === 1) {
					uni.navigateTo({
						url: `/pages/index/detail?id=${item.id}`
					})
				} else if (num === 2) {
					uni.navigateTo({
						url: `/pages/index/guide?title=${item.title}&content=${item.content}`
					})
				}
			},
			addBase(path) {
				return addBase(path)
			},
		}
	}
</script>

<style lang="scss">
	.container {
		max-width: 100%;
		padding: 0 16px;
		background-color: #f9f9f9;
	}

	.block {
		margin-bottom: 16px;

		.img {
			width: 100%;
			height: 200px;
			object-fit: cover;
			border-radius: 8px;
		}
	}

	.hot {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin-bottom: 16px;

		.card {
			width: calc(50% - 8px);
			background-color: #fff;
			border-radius: 8px;
			box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
			margin-bottom: 16px;
			overflow: hidden;
			transition: transform 0.3s ease;
		}

		.card:hover {
			transform: translateY(-4px);
		}

		.jiaolian {
			width: 100%;
			height: 150px;
			object-fit: cover;
		}

		.info {
			padding: 8px;

			.name {
				font-weight: bold;
				margin-bottom: 4px;
			}
		}
	}

	.guide {
		.list-item {
			display: flex;
			align-items: center;
			background-color: #fff;
			border-radius: 8px;
			box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
			margin-bottom: 16px;
			padding: 8px;
			transition: transform 0.3s ease;
		}

		.list-item:hover {
			transform: translateY(-4px);
		}

		.viewImg {
			width: 25%;
			height: 80px;
			border-radius: 8px;
			margin-right: 12px;
			object-fit: cover;
		}

		.line {
			width: 75%;

			.title {
				font-weight: bold;
				margin-bottom: 4px;
			}

			.three {
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 3;
				overflow: hidden;
				text-overflow: ellipsis;
				color: #666;
			}
		}
	}

	.section-title {
		font-size: 18px;
		font-weight: bold;
		color: #333;
		margin: 16px 0;
		border-bottom: 2px solid #eee;
		padding-bottom: 8px;
	}
</style>